﻿namespace TestTool.HttpTransport
{
    using System;
    using System.Collections.Generic;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using TestTool.HttpTransport.Interfaces;

    public class HttpBinding : Binding
    {
        private List<IChannelController> _controllers;
        private ControlledTextMessageBindingElement _encoding;
        private TestTool.HttpTransport.HttpTransportBindingElement _transport;

        public HttpBinding() : this(null)
        {
        }

        public HttpBinding(IEnumerable<IChannelController> elements)
        {
            this._controllers = new List<IChannelController>(elements);
            this.Initialize();
        }

        public override BindingElementCollection CreateBindingElements()
        {
            return new BindingElementCollection { this._encoding, this._transport }.Clone();
        }

        private void Initialize()
        {
            List<IChannelController> controllers = new List<IChannelController>();
            List<IChannelController> list2 = new List<IChannelController>();
            foreach (IChannelController controller in this._controllers)
            {
                ITransportController item = controller as ITransportController;
                IEncodingController controller3 = controller as IEncodingController;
                if (item != null)
                {
                    controllers.Add(item);
                }
                if (controller3 != null)
                {
                    list2.Add(controller3);
                }
            }
            this._transport = new TestTool.HttpTransport.HttpTransportBindingElement(controllers);
            this._encoding = new ControlledTextMessageBindingElement();
            this._encoding.AddControllers(list2);
        }

        public override string Scheme
        {
            get
            {
                return "http";
            }
        }

        public EnvelopeVersion SoapVersion
        {
            get
            {
                return EnvelopeVersion.Soap12;
            }
        }
    }
}

